package org.soarsky.wealthchengdu.google.map;

import java.util.ArrayList;

public class AverageValue {
	private int mMaxNum = 0, mIndex = -1;
	private ArrayList<Double> mList = null;
	
	public AverageValue(int maxNum) {
		mMaxNum = maxNum;
		if (maxNum > 0) {
			mList = new ArrayList<Double>();
		}
	}
	
	public void add(Double value){
		if (mList == null || value == null) return;
		if (mList.size() < mMaxNum){
			mList.add(value);
		}else{
			mIndex ++;
			if (mIndex >= mMaxNum) mIndex = 0;
			mList.set(mIndex, value);
		}
	}
	
	public Double getAveValue(){
		if (!isFull()) return null;
		double ave = 0;
		for(Double value : mList){
			ave += value;
		}
		return ave/mMaxNum;
	}
	public Float getAngleAveValue(){
		//if (!isFull()) return null;
		float ave = 0;
		for(Double value : mList){
			ave += value;
		}
		return ave/mList.size();
	}
	
	public boolean isFull(){
		if (mList == null || mList.size() < mMaxNum) return false;
		return true;
	}
	public void clear(){
		if (mList != null){
			mList.clear();
		}
	}
	public void free(){
		if (mList != null){
			mList.clear();
			mList = null;
		}
	}
}
